import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:ost_flutter_module/common/adapt.dart';
import 'package:ost_flutter_module/common/color.dart';

class GTextField extends StatefulWidget {
  final String initiaValue, hintText;
  // 输入最大位数，若是小数，则小数位不计算在内
  final int maxLength, maxLine;
  final bool autoFocus;
  final TextInputType keyboardType;
  // 对键盘输入的限制，自定义键盘不适用
  final List<TextInputFormatter> inputFormatters;

  final bool textFieldCenter;

  const GTextField({
    Key key,
    this.initiaValue,
    this.hintText,
    this.maxLength,
    this.autoFocus = false,
    this.keyboardType = TextInputType.multiline,
    this.inputFormatters,
    this.maxLine,
    this.textFieldCenter = false,
  }) : super(key: key);

  @override
  GTextFieldState createState() => GTextFieldState();
}

class GTextFieldState extends State<GTextField> {
  static const double height = 20;

  TextEditingController _controller;
  FocusNode _focusNode;

  bool get hasFocus => _focusNode.hasFocus;
  String get textValue => _controller.text;

  @override
  void initState() {
    super.initState();
    _controller = TextEditingController();
    _focusNode = FocusNode();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      constraints: BoxConstraints(maxHeight: height * 3, minHeight: height),
      child:
          widget.textFieldCenter ? Center(child: _textField()) : _textField(),
    );
  }

  Widget _textField() => TextField(
        controller: _controller,
        autofocus: widget.autoFocus,
        focusNode: _focusNode,
        showCursor: true,
        maxLines: widget.maxLine,
        // readOnly: widget.customKeyboard,
        // textAlign: TextAlign.end,
        style: TextStyle(
          fontSize: Adapt.px(16),
          color: color666666,
        ),
        decoration: InputDecoration(
          isCollapsed: true,
          enabledBorder: InputBorder.none,
          focusedBorder: InputBorder.none,
          hintText: widget.hintText ?? "请输入",
          hintStyle: TextStyle(fontSize: Adapt.px(16), color: color999999),
          labelText: widget.initiaValue,
          labelStyle: TextStyle(fontSize: Adapt.px(16), color: color303030),
        ),
        keyboardType: widget.keyboardType,
        inputFormatters: widget.inputFormatters,
      );
}
